package com.edu.psychological_test.service.impl;

import com.edu.psychological_test.mapper.MenuMapper;
import com.edu.psychological_test.mapper.UserMapper;
import com.edu.psychological_test.pojo.Menu;
import com.edu.psychological_test.pojo.User;
import com.edu.psychological_test.pojo.UserExample;
import com.edu.psychological_test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    MenuMapper menuMapper;

    @Override
    public User getUser(String username, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserNameEqualTo(username).andUserPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        return users.size()>0?users.get(0):null;
    }

    @Override
    public Menu selectMenuByRoleId(Integer id) {
        List<Menu> menus = menuMapper.selectByRoleId(id);
        Menu menu1 = new Menu();
        List<Menu> towMenu = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getMenuGrade().equals(0)){
                menu1=menu;
            }else {
                towMenu.add(menu);
            }
        }
        menu1.setTwomenu(towMenu);
        return menu1;
    }
}
